/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 *
 * $Id: IOServer.hpp,v 1.7 2009-09-23 11:56:14 morsko Exp $
 *
 * \file
 * \brief MaCI - IO Server interface
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 */
#ifndef _MACI_INTERFACE_IOSERVER_HPP_
#define _MACI_INTERFACE_IOSERVER_HPP_

#include "MaCI.hpp"
#include "IOData.hpp"
#include "thread.hpp"
#include "sync.hpp"
#include <list>

// Forward declare gimi::GIMI
namespace gimi {
  class GIMI;
  class GIMIMessage;
}


namespace MaCI{
  namespace IO{

    /** MaCI - IO Server interface
     * 
     * This class contains the methods and types for providing
     * the IO service in the GIMnet.
     */
    class CIOServer : public MaCI::CMaCI
    {
    public:

      /** Constructor.
       * 
       * Constructor of the IOServer. This requires;
       * a pointer to Initialized and connected GIMI, minor
       * number of this interface (Used as GIMI minor number)
       *
       * @param[in] aMaCICtrlServerPtr Pointer to GIMI used for communication
       * @param[in] aInterfaceMinor Interface minor number. Usually
       * used to indicate the instance of devices of same
       * type.
       */
      CIOServer(MaCICtrl::CMaCICtrlServer *aMaCICtrlServerPtr, const int aInterfaceMinor);

      /** Destructor.
       */
      ~CIOServer();

      /** Opens IO Server
       *
       * Opens the IO Server,  basicly registers device to GIMI.
       *  
       * @return                Returns a value of type EMaCIError. 
       *                        
       * \see EMaCIError
       */
      MaCI::EMaCIError DoOpen(void);

      /** Close device.
       *
       * Closes the device. Closes all GIMI registrations and queues.
       *
       * @return                Returns a value of type EMaCIError.
       *
       * \see EMaCIError
       */
      MaCI::EMaCIError DoClose(void);

      /** Wait for a Command.
       *
       * This function waits until new command arrives or timeout occurs.
       * If there is unhandled commands since last read, the next
       * in queue is returned. If you want to clear the queue between calls,
       * use ClearCommandBuffer() function.
       *
       * When a command is succesfully returned, the user can inspect
       * the current command through the GetCommand() call of
       * class CIOData. Then, according to command and its
       * specification, additional datafields may be present, they
       * can be queried by similar Get... functions. See the 
       * description of the data encoder/decoder module CIOData.
       *
       * 
       * \see ClearCommandBuffer()
       * \see CIOData
       * \see MaCI::EMaCIError
       * 
       * @param[out] aData      Reference to IOData. This will
       *                        be filled with incoming command when
       *                        this call is completed succesfully.
       * @param[out] aInfo      Reference to replyInfo. Use this replyInfo 
       *                        to SendReply-function when you have to send 
       *                        a Reply.
       * @param[in] aTimeout_ms Timeout to wait until operation fails
       *                        for timeout.
       * @return                'KMaCIOK'  when succesfully received
       *                        a new command. KMaCITimeout when operation
       *                        timed out before a command was received.
       *                        Other errors returned when appropriate.
       *                        (See the MaCI::EMaCIError for description)
       * \see EMaCIError
       */
      MaCI::EMaCIError WaitCommand(CIOData &aData, 
				   TReplyInfo &aInfo,
                                   const unsigned int aTimeout_ms);

      /** Sends reply message to client
       *
       * This function sends synchronous reply message to client.
       *
       * @param[in] aData       Reference to IOData. This data will be sent to
       *                        client for reply
       * @param[in] aInfo       Reference to replyInfo. Get this replyinfo from
       *                        WaitCommand.
       * @return                'KMaCIOK' when reply message succesfully sent.
       *                        Other errors returned when appropriate.
       *                        (See the MaCI::EMaCIError for description)
       * \see EMaCIError
       */
      MaCI::EMaCIError SendReply(CIOData &aData,
				 TReplyInfo &aInfo);

     
    private:

      volatile bool iServerActive; ///< Flag indicating whether the server is active.

      std::list<CIOData *> iIODataArray; ///< Array of unprocessed commands.


    };

  }

}

#endif ///< _MaCI_INTERFACE_IOSERVER_HPP_
